package com.juku2024.juku.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.juku2024.juku.R
import com.juku2024.juku.adapter.CategorySearchKeyAdapter
import com.juku2024.juku.callback.OnSearchKeySelectedListener
import com.juku2024.juku.model.CategorySearchTagModel
import java.util.*


class CategorySearchKeyView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    private val keyRecycler: RecyclerView
    var onSearchKeySelected: OnSearchKeySelectedListener? = null
    init {
        orientation = VERTICAL
        val rootView = LayoutInflater.from(context).inflate(R.layout.view_category_key, this, true)
        keyRecycler = rootView.findViewById(R.id.keyRecycler)
    }

    fun init(category: ArrayList<CategorySearchTagModel>?) {
        if (category.isNullOrEmpty()) return
        keyRecycler.adapter = null
        keyRecycler.removeAllViews()
        keyRecycler.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        val adapter = CategorySearchKeyAdapter(category)
        adapter.onSearchKeySelected = object : OnSearchKeySelectedListener {

            override fun onSearchKeySelected(
                keyId: Int,
                classes: Int,
                keyModel: CategorySearchTagModel.CategorySearchItemModel
            ) {
                onSearchKeySelected?.onSearchKeySelected(keyId, classes, keyModel)
            }
        }
        keyRecycler.adapter = adapter
    }

}